<?php
/**
 * Created by PhpStorm.
 * User: moonlight
 * Date: 2015/6/14
 * Time: 15:08
 */

namespace Admin\Model;


use Think\Model;

class UserModel extends Model{
    public function login($data){
        $Manager = M('Manager');
        $managerInfo['manager_name']   =   $data['manager_name'];
        $managerInfo['manager_password'] = sha1($data['manager_password']);
        $result = $Manager->where($managerInfo)->find();
        if($result){
            //dump($result);
            session('manager_name',$result['manager_name']);
            session('manager_id',$result['manager_id']);
            return true;
        }else{
            return false;
        }
    }
    public function getUserLists(){
        $page = I('p',1,'int');
        $user_list = $this->order('last_time desc')->page($page,10)->select();
        $count = $this->count();//获取总条数
        $Page = new\Think\Page($count,10);
        $show = $Page->show();
        return array("lists" => $user_list, "page" => $show);
    }
    public function getBlogLists(){
        $blog = M('blog');
        $page = I('p',1,'int');
        $blog_list = $blog->order('blog_date desc')->page($page,10)->select();
        $count = $blog->count();//获取总条数
        $Page = new\Think\Page($count,10);
        $show = $Page->show();
        return array("lists" => $blog_list, "page" => $show);
    }
    public function infoDelete($data){
        if($data['typeId'] == 1){//删除用户信息
            $condition['user_id']   =   $data['id'];
            $result1    =   M('reply')->where($condition)->delete();
            $result2    =   M('blog')->where($condition)->select();
            if($result2){
                $count = count($result2);
                $i = 0;
                while($i<$count){
                    $condition1['blog_id'] = $result2[$i]['blog_id'];
                    $result3    =   M('goods')->where($condition1)->delete();
                    $i++;
                }
            }
            $result4 = M('blog')->where($condition)->delete();
            $result = $this->where($condition)->delete();
            if($result){
                return 1;
            }
            return false;
        }
        if($data['typeId'] == 2){//删除blog
            $condition['blog_id']   =   $data['id'];
            $result1    =   M('reply')->where($condition)->delete();
            $result2    =   M('goods')->where($condition)->delete();
            $result4    =   M('blog')->where($condition)->delete();
            if($result4){
                return 2;
            }
            return false;
        }
    }
}